#include "interfacepainter.h"

using namespace Shiny;

InterfacePainter::InterfacePainter(QWidget *parent) :
    QSFMLCanvas(parent)
{
    _ui             = 0;
    _textureManager = 0;
}

InterfacePainter::~InterfacePainter()
{
    if (_ui)
      delete _ui;
}

void InterfacePainter::LoadUi(Data data)
{
      if (_ui)
        delete _ui;
      _ui = new Widgets::Ui(data, _textureManager, 0);
}

void InterfacePainter::NewUi(void)
{
    if (_ui)
      delete _ui;
    _ui = new Widgets::Ui(_textureManager, 0);
}

void InterfacePainter::OnUpdate()
{
    this->Clear(sf::Color(255.f, 255.f, 255.f, 0.f));
    if (_ui)
      _ui->Render(*this);
    QSFMLCanvas::OnUpdate();
}

void InterfacePainter::mousePressEvent(QMouseEvent* event)
{
    sf::Event sfevent;

    sfevent.Type          = sf::Event::MouseButtonPressed;
    sfevent.MouseButton.X = event->pos().x();
    sfevent.MouseButton.Y = event->pos().y();
    _ui->mouseEvent(sfevent);
}

void InterfacePainter::mouseReleaseEvent(QMouseEvent* event)
{
    sf::Event sfevent;

    sfevent.Type = sf::Event::MouseButtonPressed;
    sfevent.MouseButton.X = event->pos().x();
    sfevent.MouseButton.Y = event->pos().y();
    _ui->mouseEvent(sfevent);
}
